package com.etrans.web.gps.business.video.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etrans.egov.base.intf.ObjDriver;
import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.video.domain.TrackVO;
import com.etrans.web.gps.business.video.domain.VehicleVO;
import com.etrans.web.gps.business.video.domain.VideoAlarmVO;
import com.etrans.web.gps.business.video.domain.VideoVO;
import com.etrans.web.gps.util.Config;
import com.etrans.web.gps.util.EgovUtil;
import com.etrans.web.gps.util.map.GpsToGaoDe;


@Service
public class VideoService extends BaseService {
	private static final Logger  logger =LoggerFactory.getLogger(VideoService.class);

	@Autowired
    private EgovUtil egovUtil;
	
	@Autowired
	private Config config;
	
    /**
     * 查询车辆视频参数信息
     *
     * @param queryParam
     * @return
     */
    public VideoVO queryVehicleDeviceSetup(Map<String,Object> queryParam){
    	VideoVO video=null;
        List<VideoVO> list = selectList("video.queryVehicleDeviceSetup",queryParam);
        if(null != list && list.size()>0){
        	video=list.get(0);
        }
        return video;
    }
    
    /**
     * 查询当前用户有视频的车辆列表
     *
     * @param queryParam
     * @return
     */
    public List<VehicleVO> queryVideoVehicleList(Map<String,Object> queryParam){
        List<VehicleVO> list = selectList("video.queryVideoVehicleList",queryParam);
        return list;
    }
    
    
    /**
     * 判断车辆是否有视频
     *
     * @param queryParam
     * @return
     */
    public List<VehicleVO> queryVideoVehicleIsOn(Map<String,Object> queryParam){
        List<VehicleVO> list = selectList("video.queryVideoVehicleIsOn",queryParam);
        return list;
    }
    
    /**
     * 查询当前车辆轨迹列表
     *
     * @param queryParam
     * @return
     */
    public List<TrackVO> queryVideoVehicleTrackList(Map<String,Object> queryParam){
        List<TrackVO> list = selectList("video.queryVideoVehicleTrackList",queryParam);
        return list;
    }
    
    
    /**
     * 根据ID，时间段查询疲劳报警
     *
     * @param queryParam
     * @return
     */
    public List<VideoAlarmVO> queryVideoTiredAlarmList(Map<String,Object> queryParam){
        List<VideoAlarmVO> list = selectList("video.queryVideoTiredAlarmList",queryParam);
        if(null != list && list.size()>0){
        	for(VideoAlarmVO temp : list){
        		double[] lnglats=GpsToGaoDe.transform(temp.getsLat()/1000000, temp.getsLon()/1000000);
        		double[] lnglate=GpsToGaoDe.transform(temp.geteLat()/1000000, temp.geteLon()/1000000);
        		temp.setsLat(lnglats[0]);
        		temp.setsLon(lnglats[1]);
        		temp.seteLat(lnglate[0]);
        		temp.seteLon(lnglate[1]);
        	}
        }
        return list;
    }
    
    
    /**
     * 根据ID，时间段查询夜间行车
     *
     * @param queryParam
     * @return
     */
    public List<VideoAlarmVO> queryVideoNightAlarmList(Map<String,Object> queryParam){
        List<VideoAlarmVO> list = selectList("video.queryVideoNightAlarmList",queryParam);
        if(null != list && list.size()>0){
        	for(VideoAlarmVO temp : list){
        		double[] lnglats=GpsToGaoDe.transform(temp.getsLat()/1000000, temp.getsLon()/1000000);
        		double[] lnglate=GpsToGaoDe.transform(temp.geteLat()/1000000, temp.geteLon()/1000000);
        		temp.setsLat(lnglats[0]);
        		temp.setsLon(lnglats[1]);
        		temp.seteLat(lnglate[0]);
        		temp.seteLon(lnglate[1]);
        	}
        }
        return list;
    }
    
    
    /**
     * 根据ID，时间段查询超速报警
     *
     * @param queryParam
     * @return
     */
    public List<VideoAlarmVO> queryVideoSpeedAlarmList(Map<String,Object> queryParam){
        List<VideoAlarmVO> list = selectList("video.queryVideoSpeedAlarmList",queryParam);
        if(null != list && list.size()>0){
        	for(VideoAlarmVO temp : list){
        		double[] lnglats=GpsToGaoDe.transform(temp.getsLat()/1000000, temp.getsLon()/1000000);
        		double[] lnglate=GpsToGaoDe.transform(temp.geteLat()/1000000, temp.geteLon()/1000000);
        		temp.setsLat(lnglats[0]);
        		temp.setsLon(lnglats[1]);
        		temp.seteLat(lnglate[0]);
        		temp.seteLon(lnglate[1]);
        	}
        }
        return list;
    }
    
    /**
     * 根据ID，时间段查询道路类型超速
     *
     * @param queryParam
     * @return
     */
    public List<VideoAlarmVO> queryVideoRoadLevelSpeedAlarmList(Map<String,Object> queryParam){
        List<VideoAlarmVO> list = selectList("video.queryVideoRoadLevelSpeedAlarmList",queryParam);
        if(null != list && list.size()>0){
        	for(VideoAlarmVO temp : list){
        		double[] lnglats=GpsToGaoDe.transform(temp.getsLat()/1000000, temp.getsLon()/1000000);
        		double[] lnglate=GpsToGaoDe.transform(temp.geteLat()/1000000, temp.geteLon()/1000000);
        		temp.setsLat(lnglats[0]);
        		temp.setsLon(lnglats[1]);
        		temp.seteLat(lnglate[0]);
        		temp.seteLon(lnglate[1]);
        	}
        }
        return list;
    }

	public ObjDriver getDrivserInfo(String[] vehicle_no_color,HttpServletRequest request) throws Exception{
		ObjDriver driverInfo = egovUtil.getDriverInfo(vehicle_no_color);
		if(null!=driverInfo&&null!=driverInfo.getLicence()){
			String licence = driverInfo.getLicence();
			List<HashMap<String, Object>> imgurlList = selectList(
					"video.queryDriverPhoto", licence);
			if (null != imgurlList && imgurlList.size() > 0) {
				for (HashMap<String, Object> driver : imgurlList) {
					if (null != driver.get("img_url")
							&& "" != driver.get("img_url")) {// 图片服务器图片
						driverInfo.setImgUrl(config.imagePath
								+ String.valueOf(driver.get("img_url")));
					}
					else{
						driverInfo.setImgUrl(request.getContextPath()
								+ "/resources/images/touxiang.jpg");
					}
				}
			} else {
				driverInfo.setImgUrl(request.getContextPath()
						+ "/resources/images/touxiang.jpg");
			}
		}
		 else {
			 logger.info("暂无司机信息！");
			}
		return driverInfo;
	}
	
	/**
     * 查询车辆Flash视频参数信息
     *
     * @param queryParam
     * @return
     */
    public VideoVO queryVehicleVideoInfo(Map<String,Object> queryParam){
    	VideoVO video=null;
        List<VideoVO> list = selectList("video.queryVehicleVideoInfo",queryParam);
        if(null != list && list.size()>0){
        	video=list.get(0);
        }
        return video;
    }
}
